package edu.isistan.metamodel;

import java.util.Vector;

/**
 * Abstract class for DataType's common attributes
 */
public abstract class DataType extends ModelElement {
	
	protected String type;
	protected String doc;
	protected DataType containedIn = null;
	protected int cardinality = 0;
	private Collection partOf = null;
	
	public abstract boolean isPrimitive();
	public abstract boolean isCollection();
	public abstract boolean isWrapper();
	public abstract boolean isCompoused();
	public abstract boolean isArray();	

	public DataType(String name, String type, String doc) {
		super();
		this.name = name;
		this.type = type;
		this.doc = doc;
	}
	public DataType() {
		this.name = null;
		this.type = null;
		this.doc = null;
	}
	
	public String getPrettyPrintType() {
		Vector<DataType> parents = new Vector<DataType>();
		DataType aux = this;
		while (aux.getContainedIn() != null) {
			parents.add(aux.getContainedIn());
			aux = aux.getContainedIn();
		}
		StringBuffer sb = new StringBuffer();
		for (int i=parents.size()-1; i>=0;i--) { 
			DataType parent = parents.get(i);			
			if (parent.getPartOf()!=null)
				sb.append( parent.getPartOf().getPrettyPrintType() ); 
			else 
				sb.append(parent.getPrettyPrintType());
			sb.append("#");
		}
		sb.append(getType());
		return sb.toString();
	}
	
	public String getMetamodelChain() {
		Vector<DataType> parents = new Vector<DataType>();
		DataType aux = this;
		while (aux.getContainedIn() != null) {
			parents.add(aux.getContainedIn());
			aux = aux.getContainedIn();
		}
		StringBuffer sb = new StringBuffer();
		for (int i=parents.size()-1; i>=0;i--) { 
			DataType parent = parents.get(i);			
			sb.append(parent.getClass().getName());
			sb.append("#");
		}
		sb.append(this.getClass().getName());
		return sb.toString();
	}
	
	
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getDoc() {
		return doc;
	}
	public void setDoc(String doc) {
		this.doc = doc;
	}
	public String getType() {
		return type;
	}
	public void setType(String type) {
		this.type = type;
	}
	public DataType getContainedIn() {
		return containedIn;
	}
	public void setContainedIn(DataType containedIn) {
		this.containedIn = containedIn;
	}
	public Collection getPartOf() {
		return this.partOf;
	}
	public void setPartOf(Collection partOf) {
		this.partOf = partOf;
	}
	public int getCardinality() {
		return cardinality;
	}
	public void setCardinality(int cardinality) {
		this.cardinality = cardinality;
	}
	@Override
	public String toString() {
		StringBuffer sb=new StringBuffer();
		sb.append("(type=");
		sb.append(getType());
		sb.append(";name=");
		sb.append(getName());
		sb.append(")");
		return sb.toString();
	}
}
